/* eslint-disable import/no-named-as-default-member */
import { DeviceInfo, BindDeviceProps, BPData, AvailableState } from "./type";
import Taro from "@tarojs/taro";

// const lzbluetooth = Taro.requirePlugin('lzbluetooth');

import lzbluetooth from "sg-ble-next";

export enum EventName {
  AdaptorState = "adaptorState",
  ConnectionState = "connectionState",
  DataReport = "dataReport"
}

export interface AdaptorStateEventCallback {
  eventName: EventName.AdaptorState;
  eventKey: string;
  callback: (res: AvailableState) => void;
}

export interface ConnectionStateResult {
  mac: string;
  deviceState: number;
}

export interface ConnectionStateResultCallback {
  eventName: EventName.ConnectionState;
  eventKey: string;
  callback: (mac: string, deviceState: number) => void;
}

export interface DataReportResultCallback {
  eventName: EventName.DataReport;
  eventKey: string;
  callback: (deviceInfo: DeviceInfo, reportData: BPData) => void;
}
export type EventCallback =
  | AdaptorStateEventCallback
  | ConnectionStateResultCallback
  | DataReportResultCallback;

function privateOnBluetoothDeviceFound(obj) {
  console.warn("privateOnBluetoothDeviceFound");
  Taro.onBluetoothDeviceFound(res => {
    console.warn("privateOnBluetoothDeviceFound", res);
    obj(res);
  });
}

function privateOnBLECharacteristicValueChange(obj) {
  console.warn("privateOnBLECharacteristicValueChange");
  Taro.onBLECharacteristicValueChange(res => {
    console.warn("privateOnBLECharacteristicValueChange", res);
    obj(res);
  });
}

function privateOnBLEConnectionStateChange(obj) {
  console.warn("privateOnBLEConnectionStateChange");
  Taro.onBLEConnectionStateChange(res => {
    console.warn("privateOnBLEConnectionStateChange", res);
    obj(res);
  });
}

function privateOnBluetoothAdapterStateChange(obj) {
  console.warn("privateOnBluetoothAdapterStateChange");
  Taro.onBluetoothAdapterStateChange(res => {
    console.warn("privateOnBluetoothAdapterStateChange", res);
    obj(res);
  });
}

/** 初始化SDK */
export function initLzbluetooth() {
  console.log("插件版本号", lzbluetooth.getVersion());
  lzbluetooth.init({
    appId: "com.leshiguang.saas.rbac.demo.appid",
    onBluetoothDeviceFound: privateOnBluetoothDeviceFound,
    onBLECharacteristicValueChange: privateOnBLECharacteristicValueChange,
    onBLEConnectionStateChange: privateOnBLEConnectionStateChange,
    onBluetoothAdapterStateChange: privateOnBluetoothAdapterStateChange
  });
}
/**
 * 扫描设备
 */
export function startScanning(options: any) {
  lzbluetooth.startScanning(options);
}

/**
 * 停止扫描
 */
export function stopScanning() {
  lzbluetooth.stopScanning();
}

/**
 * 绑定设备
 * @param { mac, callback } options
 */
export function bindDevice(options: BindDeviceProps) {
  return lzbluetooth.bindDevice(options);
}

/**
 * 设备的连接情况
 * @param { mac } options
 * @returns 根据设备信息得到设备设备的连接情况
 */
export function getConnectionState({ mac }: { mac: string }) {
  return lzbluetooth.getConnectionState({ mac });
}

/**
 * 判断蓝牙是否可用
 * @returns boolean 蓝牙是否可用
 */
export function isBluetoothAvailable() {
  return lzbluetooth.isBluetoothAvailable();
}

/**
 * 增加监听事件
 * @param string eventName 事件名称
 * @param string eventKey 监听者的标识，去除监听的时候需要使用到这个key 找到监听者
 * @param {*} callback 回调
 */
export function addListener({ eventName, eventKey, callback }: EventCallback) {
  lzbluetooth.$on({
    eventName,
    eventKey,
    callback
  });
}

/**
 * 删除监听事件
 * @param string eventName
 * @param string eventKey 标识，同一标识的监听会被覆盖
 */
export function removeListener({
  eventName,
  eventKey
}: {
  eventName: string;
  eventKey: string;
}) {
  lzbluetooth.$off({
    eventName,
    eventKey
  });
}

interface MonitorDeviceProps {
  mac: string;
  /** 设备型号 */
  model: string;
}

/**
 * 添加监听设备 可以是数组
 * @param { mac, model } device
 */
export function addMonitorDevice(device: MonitorDeviceProps) {
  lzbluetooth.addMonitorDevice(device);
}

/**
 * 设置所有的监听的设备,相对于 addMonitor 这个是替换 可以是数组
 * @param { mac, model} devices
 */
export function setMonitorDevice(
  devices: MonitorDeviceProps[] | MonitorDeviceProps
) {
  lzbluetooth.setMonitorDevice(devices);
}

/**
 * 删除监听的设备
 * @param {*} device
 */
export function deleteMonitorDevice(mac: string) {
  lzbluetooth.deleteMonitorDevice({ mac });
}

/**
 * 删除所有的监听的设备
 */
export function deleteAllMonitorDevice() {
  lzbluetooth.deleteAllMonitorDevice();
}

/** 解绑设备 */
export function cancelBind(mac: string) {
  lzbluetooth.cancelBind(mac);
}

/** 根据型号获取产品设备名称 */
export function getDeviceNameOfModel(model: string) {
  let deviceName = "";
  switch (model) {
    case (deviceName = "乐心双管血压计i7蓝牙版"):
      break;
  }
  return deviceName;
}
